/**
 * 定义监测仪器管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.apps.WasteWater.DataComparison.dataComparisonManager', ['water.entity.dataComparison','water.common.dateUtil']);
	
	module.controller('fsdataComparisonManagerController',function($rootScope,$scope,$dataComparisonService,$controller,$monitorStationService,$dateUtil,$state){     
		/**
		 * 上传对话框对象
		 */
		$scope.uploadDialog;
		/**
		 * 查询条件参数集
		 */
		$scope.queryObject={};
		/**
		 * 总展示页面数据集
		 */
		$scope.dataComparisonList = {};
		/**
		 * 详情页面，默认显示报告预览页面
		 */
		$scope.detailQuery=0;
		/**
		 * 新建页面数据集
		 */
		$scope.dataComparisonCreate = {};
		/**
		 * 编辑页面数据集
		 */
		$scope.dataComparisonEdit = {};
		/**
		 * 上传页面数据集
		 */
		$scope.dataComparisonUpload = {};
		/**
		 * 数据比对详细界面数据、以报告编号+参数为主键
		 */
		$scope.Comparisondata = {};
		/**
		 * 删除事件
		 */
		$scope.onDelClick;
		
		/**
		 * 新增按钮点击事件
		 */
		$scope.onCreateClick = function(){
			//根据STATION_ID查询相应的MN号及所属企业名称
			var queryObject = {
					"STATION_ID":$scope.selectedModel.STATION_ID,
				};
			$dataComparisonService.getBaseInfoByStationID(queryObject).then(function(data){
				$scope.dataComparisonCreate = {};
				$scope.getCreateParms("");
				$scope.dataComparisonCreate.COMPANYNAME = data.data[0].COMPANYNAME;
				$scope.dataComparisonCreate.POINTNAME  = data.data[0].POINTNAME;
				$scope.dataComparisonCreate.MN = data.data[0].MN;
				$scope.createDialog.open();
			});	
		}
		
		/**
		 * 新建页面提交新建按钮点击事件
		 */
		$scope.onSubmitCreateClick = function(data){
			var DATACOMPARISONINFO_LIST = [];
			//新建时候处理，若parmCode为空，则选择全部参数
			if($scope.dataComparisonCreate.PARMCODE =="")
			{
				$scope.dataComparisonCreate.PARMCODE = $scope.selectparmList;
			}			
			
			var parm=$scope.dataComparisonCreate.PARMCODE.split(",");
			for(var i=0;i<parm.length;i++){
				if(parm[i]!="undefined"&&parm[i]!=""){
					$scope.Comparisondata={};
					$scope.Comparisondata.PARMNAME=parm[i];
					$scope.Comparisondata.REPORTNUMBER=data.REPORTNUMBER;
					DATACOMPARISONINFO_LIST.push($scope.Comparisondata);
				}			
			}
			data.DATACOMPARISONINFO_LIST = DATACOMPARISONINFO_LIST;
			$dataComparisonService.createComparisonSummaryModel(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope.getDataComparisonSummaryList();
				$scope.createDialog.close();
			});
		};
		
		/**
		 * 查看按钮事件，用于切换页面
		 */
		$scope.onViewDetail = function(station){
			$state.go("DataComparison.TaskDetailQuery",{curRow:station});
		}
		
		/**
		 * 下载按钮事件，用于导出word
		 */
		$scope.onDownWord = function(queryObj){
			$dataComparisonService.exportDataComparison(queryObj);
		}
		
		/**
		 * 编辑按钮点击事件
		 */
		$scope.onEditClick = function(curRow){
			$scope.dataComparisonEdit= angular.copy(curRow);
			$scope.dataComparisonEdit.PARMCODE += ",";
			var parm=$scope.dataComparisonEdit.PARMCODE.split(",");
			$scope.dataComparisonEdit.PARMCODE = "";
			//先移除parmsUl已选择的参数
			$("#parmsUl li").removeClass('selected');
			for(var i=0;i<parm.length-1;i++){
				$scope.getEditParms(parm[i]);
			}
			$scope.editDialog.open();
		}
		/**
		 * 删除按钮点击事件
		 */
		$scope.onDelClick=function(curRow){		
			$.messager.confirm("确认","确定要删除这条记录吗？",function(r){
				if(r){
					$dataComparisonService.deleteDataComparison(curRow);
					$scope.getDataComparisonSummaryList();
				}
			});
		}
		/**
		 * 提交新建按钮点击事件
		 */
		$scope.onSubmitEditClick = function(editData){
			var DATACOMPARISONINFO_LIST = [];
			//新建时候处理，若parmCode为空，则选择全部参数
			if($scope.dataComparisonEdit.PARMCODE =="")
			{
				$scope.dataComparisonEdit.PARMCODE = $scope.selectedModel.parmToChecksStr;
			}			
			//根据报告编号，查出已有的参数记录AEComparisondata
			$scope.queryObject.REPORTNUMBER = $scope.dataComparisonEdit.REPORTNUMBER;
			$scope.queryObject.STATIONCODE = $scope.selectedModel.STATION_ID;
			$dataComparisonService.getComparisonList($scope.queryObject).then(function(data){
				$scope.AEComparisondata=data.data;
				var parm=$scope.dataComparisonEdit.PARMCODE.split(",");
				//添加与更新dataComparison数据
				for(var i=0;i<parm.length-1;i++){
					$scope.Comparisondata={};
					$scope.Comparisondata.REPORTNUMBER=$scope.dataComparisonEdit.REPORTNUMBER;
					$scope.Comparisondata.PARMNAME=parm[i];
					//若已存在该参数的记录，则把把现有记录赋值,以进行update操作
					for(var j=0;j<$scope.AEComparisondata.length;j++)
					{
						if($scope.AEComparisondata[j].PARMNAME==parm[i])
						{
							$scope.Comparisondata=$scope.AEComparisondata[j];
						}
					}
					DATACOMPARISONINFO_LIST.push($scope.Comparisondata);
				}
				
				$scope.dataComparisonEdit.DATACOMPARISONINFO_LIST = DATACOMPARISONINFO_LIST;
				$dataComparisonService.updateComparisonSummaryModelWithChild($scope.dataComparisonEdit).then(function(){
					$.messager.alert("保存成功","保存成功",'info');
					$scope.editDialog.close();
					$scope.getDataComparisonSummaryList();
				});
			 });				
		};
		
		/**
		 * 上传页面提交按钮点击事件
		 */
		$scope.onSubmitUploadClick = function(){
			$dataComparisonService.updateComparisonSummaryModel($scope.dataComparisonUpload).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope.getDataComparisonSummaryList();
				$scope.uploadDialog.close();
			});
		};
		
		/**
		 * 上传按钮点击事件，用于图片上传
		 */
		$scope.onUploadClick = function(curRow){
			$scope.dataComparisonUpload = angular.copy(curRow);
			$scope.uploadDialog.open();
		}
		
		/**
		 * 切换菜单的时候，把注册的事件取消掉
		 */
		$rootScope.$on('$stateChangeStart',function(event, toState, toParams, fromState, fromParams){
			$scope.oNdataPramChanged();
	 	});

		/**
		 * 监听左侧列表选择
		 */
		$scope.oNdataPramChanged = $scope.$on('dataPramChanged', function(event, station) {
			$scope.onStationSelectedBeforeQuery(station);
			$scope.getDataComparisonSummaryList();
	    });
		
		/**
		 * 站点选择后，报表查询前执行的逻辑
		 * 用于准备查询前的参数
		 */
		$scope.onStationSelectedBeforeQuery = function(station){
			$scope.selectedModel = station;
			$scope.selectparmList="";
			var arrParm=[];
			for(var i=0;i<$scope.selectedModel.parmToCheckList.length;i++){
				var obj=$scope.selectedModel.parmToCheckList[i];
				if(obj.name.indexOf("B01TOTAL") != -1||obj.name.indexOf("B01") != -1){
					
				}
				else{
					arrParm.push(obj);
					$scope.selectparmList+=","+obj.name;
				}
			}
			$scope.parmList = arrParm;	
		}
		
		/**
		 * 获取数据比对汇总列表
		 */
		$scope.getDataComparisonSummaryList=function(){
			var queryObject = {
					"MN":$scope.selectedModel.MN,
					"START":$scope.start + " 00:00:00",
					"END":$scope.end + " 00:00:00",
				};
			var li3 = $("#pointName li[orgid='"+-1+"']");
			if(li3.hasClass("selected"))
			{
				queryObject.MN = "all";
			}
			
			$dataComparisonService.getDataComparisonSummaryList(queryObject).then(function(data){
				$scope.dataComparisonList = data.data;
				console.log(data);
			});
		};

		/**
		 * 获取查询参数
		 */
		$scope.getQueryObject = function(){
			var startTime =$scope.queryObject.start;
			var endTime = $scope.queryObject.end;
			var queryObject = {
					"MN":$scope.selectedModel.MN,
				};	
			return queryObject;
		}
		
		//站点选中事件
		$scope.getPointName=function(orgid){
			var li3 = $("#pointName li[orgid='"+-1+"']");
			var li = $("#pointName li[orgid='"+orgid+"']");
			if(orgid=="")
			{
				var li1=$("#pointName li");
				li1.removeClass('selected');
				li3.addClass('selected');
				return;
			}
			if(li.hasClass("selected"))
			{
				li3.removeClass('selected');
				li.removeClass('selected');
			}
			else{
				li3.removeClass('selected');
				li.addClass('selected');
			}
			if(!$("#pointName li").hasClass("selected")){
				li3.addClass('selected');
			}
		};				
		
		//时间验证
		$scope.checkErr = function(){		
			if(typeof $scope.queryObject.start!="undefined")
			{
			    if((new Date($scope.queryObject.start.replace(/-/g,"\/"))) > (new Date($scope.queryObject.end.replace(/-/g,"\/")))){
			    	$.messager.alert("提示","开始时间不能大于结束时间");
			      return false;
			    }	
			}
		 };		

		/**
		 * 报告类型变更时候，自动生成报告编号
		 */
		$scope.onReportTypeChange = function(){
			if($scope.dataComparisonCreate.REPORTTYPE == undefined)
			{
				return;
			}		
			//验收报告szwys ,有效性报告 szwyx
			var prefix;
			var datatime = $scope.dataComparisonCreate.DATATIME.replace(/-/g,"");
			datatime = datatime.substring(2,datatime.length);
			if($scope.dataComparisonCreate.REPORTTYPE == 0)
			{   
				prefix = "szwys";
			}else if($scope.dataComparisonCreate.REPORTTYPE == 1)
			{
				prefix = "szwyx";
			}			
			var queryObject = {
					"DATATIME":$scope.dataComparisonCreate.DATATIME + " 00:00:00",
					"REPORTTYPE":$scope.dataComparisonCreate.REPORTTYPE,
				};
			$dataComparisonService.getReportNumber(queryObject).then(function(data){
				$scope.dataComparisonCreate.REPORTNUMBER = prefix + datatime + "第" + data.data[0].REPORTNUMBER + "号";
			});	
		}
		
		/**
		 * 报告类型变更时候，自动生成报告编号
		 */
		$scope.onReportTypeEditChange = function(){
			if($scope.dataComparisonEdit.REPORTTYPE == undefined)
			{
				return;
			}		
			//验收报告szwys ,有效性报告 szwyx
			var prefix;
			var datatime = $scope.dataComparisonEdit.DATATIME.replace(/-/g,"");
			datatime = datatime.substring(2,datatime.length);
			if($scope.dataComparisonEdit.REPORTTYPE == 0)
			{   
				prefix = "szwys";
			}else if($scope.dataComparisonEdit.REPORTTYPE == 1)
			{
				prefix = "szwyx";
			}			
			var queryObject = {
					"DATATIME":$scope.dataComparisonEdit.DATATIME + " 00:00:00",
					"REPORTTYPE":$scope.dataComparisonEdit.REPORTTYPE,
				};
			$dataComparisonService.getReportNumber(queryObject).then(function(data){
				$scope.dataComparisonEdit.REPORTNUMBER = prefix + datatime + "第" + data.data[0].REPORTNUMBER + "号";
			});	
		}
		//新建页面监测参数选中事件
		$scope.getCreateParms=function(orgid){
			var li3 = $("#parmsUl li[orgid='"+-1+"']");
			if(orgid=="")
			{   
				$scope.dataComparisonCreate.PARMCODE = "";
				var li1=$("#parmsUl li");
				li1.removeClass('selected');
				li3.addClass('selected');
				return;
			}
			var li = $("#parmsUl li[orgid='"+orgid+"']");
			if(li.hasClass("selected"))
			{
				li3.removeClass('selected');
				li.removeClass('selected');
				if($scope.dataComparisonCreate.PARMCODE.indexOf(orgid+",") >= 0 ){
					$scope.dataComparisonCreate.PARMCODE=$scope.dataComparisonCreate.PARMCODE.replace(orgid+",","");
				}
			}
			else{
				li3.removeClass('selected');
				li.addClass('selected');
				$scope.dataComparisonCreate.PARMCODE += orgid+",";
			}
			if(!$("#parmsUl li").hasClass("selected")){
				li3.addClass('selected');
			}
		};	
		
		//编辑页面监测参数选中事件
		$scope.getEditParms=function(orgid){
			var li3 = $("#parmsUl li[orgid='"+-1+"']");
			if(orgid=="")
			{   
				$scope.dataComparisonEdit.PARMCODE = "";
				var li1=$("#parmsUl li");
				li1.removeClass('selected');
				li3.addClass('selected');
				return;
			}
			var li = $("#parmsUl li[orgid='"+orgid+"']");
			if(li.hasClass("selected"))
			{
				li3.removeClass('selected');
				li.removeClass('selected');
				if($scope.dataComparisonEdit.PARMCODE.indexOf(orgid+",") >= 0 ){
					$scope.dataComparisonEdit.PARMCODE=$scope.dataComparisonEdit.PARMCODE.replace(orgid+",","");
				}
			}
			else{
				li3.removeClass('selected');
				li.addClass('selected');
				$scope.dataComparisonEdit.PARMCODE += orgid+",";
			}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
			if(!$("#parmsUl li").hasClass("selected")){
				li3.addClass('selected');
			}
		};	
		
		
		
	});
}(window, angular));	